package com.twu.biblioteca;

import com.twu.biblioteca.customer.Customer;
import com.twu.biblioteca.library.Library;
import com.twu.biblioteca.operation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class BibliotecaApp {
	private Library library = new Library();
	private Customer currentCustomer = new Customer("","","","");
	private Scanner scanner = new Scanner(System.in);
	private MenuOperation listBookMenuOperation = new ListBookMenuOperation();
	private MenuOperation lendBookMenuOperation = new LendBookMenuOperation();
	private MenuOperation checkMemberShipMenuOperation = new CheckMemberShipMenuOperation();
	private MenuOperation listMoviesMenuOperation = new ListMoviesMenuOperation();
	private MenuOperation loginMenuOperation = new LoginMenuOperation();
	private Map<Integer, MenuOperation> operations = new HashMap<Integer, MenuOperation>();

	public BibliotecaApp() {
		menuOperationInitialize();
	}

	private void menuOperationInitialize() {
		operations.put(1, listBookMenuOperation);
		operations.put(2, lendBookMenuOperation);
		operations.put(3, checkMemberShipMenuOperation);
		operations.put(4,listMoviesMenuOperation);
		operations.put(5,loginMenuOperation);
	}

	public void run() {
		sayWelcomeAndShowMenu();
		menuChoose();
		sayExit();
	}

	private void menuChoose() {
		int chooseNum;
		while ((chooseNum = scanner.nextInt()) != 0) {
			MenuOperation menuOperation = operations.get(chooseNum);
			if (menuOperation != null) {
				System.out.println(menuOperation.execute(library, currentCustomer));
			} else {
				sayValidOption();
			}
		}
	}

	private void sayWelcomeAndShowMenu() {
		sayWelcome();
		showMenu();
	}

	private void sayValidOption() {
		System.out.println("Select a valid option!!");
	}

	private void sayWelcome() {
		System.out.println("Welcome to BibliotecaApp");
	}

	private void showMenu() {
		System.out.println("Please choose an option:\n1.View All Books\n2.Borrow\n3.Check Membership\n4.View All Movies\n5.Login\n0.Exit");
	}

	private void sayExit() {
		System.out.println("Exit");
		scanner.close();
	}

	public static void main(String[] args) {
		BibliotecaApp bibliotecaApp = new BibliotecaApp();
		bibliotecaApp.run();
	}
}
